package com.witmed.user

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Button
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.text.input.VisualTransformation
import androidx.compose.ui.unit.dp
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.get
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.witmed.lib_common.ActivityPath
import com.witmed.lib_common.utils.ACache
import com.witmed.user.databinding.ActivityLoginBinding
import com.witmed.user.intent.UserIntent
import com.witmed.user.viewmodel.LoginState
import com.witmed.user.viewmodel.LoginViewModel
import kotlinx.coroutines.launch

@Route(path = ActivityPath.PAGE_LOGIN)
class LoginActivity : AppCompatActivity() {
    lateinit var db:ActivityLoginBinding
    lateinit var vm:LoginViewModel
    @OptIn(ExperimentalMaterial3Api::class)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        db=ActivityLoginBinding.inflate(layoutInflater)
        setContentView(db.root)

        vm=ViewModelProvider(this).get(LoginViewModel::class.java)


        lifecycleScope.launch {
            vm.loginState.collect{
                when(it){
                    is LoginState.LoginSuccess->{
                        var user=it.user
                        ACache.get(this@LoginActivity).put("user",user,7*24*60*60)
                        //ARouter.getInstance().build(ActivityPath.PAGE_MAIN).navigation()
                        finish()
                    }
                    is LoginState.Error->{
                        Toast.makeText(this@LoginActivity, it.msg, Toast.LENGTH_SHORT).show()
                    }
                    else->{}
                }
            }
        }


        db.comLogin.setContent {
            MaterialTheme {
                var name by remember {
                    mutableStateOf("li07")
                }
                var pass by remember {
                    mutableStateOf("222")
                }
                Column(horizontalAlignment=Alignment.CenterHorizontally) {
                    OutlinedTextField(shape= RoundedCornerShape(20.dp),
                        value = name,
                        label = { Text(text = "用户名")},
                        onValueChange = {name=it})
                    Spacer(modifier = Modifier.height(20.dp))
                    OutlinedTextField(shape= RoundedCornerShape(20.dp),
                        value = pass,
                        label = { Text(text = "密码")},
                        visualTransformation= PasswordVisualTransformation(),
                        onValueChange = {pass=it})
                    Spacer(modifier = Modifier.height(20.dp))
                    Button(modifier = Modifier
                        .fillMaxWidth()
                        .padding(80.dp, 0.dp),
                        shape= RoundedCornerShape(20.dp),
                        onClick = { //登录事件
                            if(name.isEmpty()||pass.isEmpty()){
                                Toast.makeText(this@LoginActivity, "输入不能为空", Toast.LENGTH_SHORT).show()
                                return@Button
                            }
                            if(!pass.matches(Regex("[0-9A-z_]{3,9}"))){
                                Toast.makeText(this@LoginActivity, "需要输入字母数字下划线3-9位", Toast.LENGTH_SHORT).show()
                                return@Button
                            }
                            lifecycleScope.launch {
                                //发送登录意图
                                vm.loginChannel.send(UserIntent.login(name,pass))
                            }
                        }) {
                        Text(text = "登录")
                    }
                    Spacer(modifier = Modifier.height(20.dp))
                    Row(horizontalArrangement = Arrangement.Start,
                        modifier = Modifier.fillMaxWidth().padding(80.dp,0.dp)) {
                        Text(text = "用户名密码注册",modifier = Modifier.clickable {
                            ARouter.getInstance().build(ActivityPath.PAGE_REGISTER).navigation()
                        })
                    }

                }

            }
        }
    }
}